<?php

namespace App\Http\Controllers;

use App\Models\Setting;
use Illuminate\Http\Request;

class SettingController extends Controller{

    /**
     * 系统配置
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(){
        $data = Setting::get()->pluck('value','key');
        return $this->returnJson(0,'',$data);
    }

    /**
     * 系统配置更新
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(Request $request){
        try {
            $data = $request->all();
            foreach ($data as $key=>$item){
                Setting::updateOrCreate(['key' => $key],['key' => $key,'value'=>$item]);
            }
            return $this->returnJson(0,'更新成功，页面即将跳转...');
        } catch (\Exception $e) {
            return $this->returnJson(1, $e->getMessage());
        }

    }

}